package com.wardrobe.soa.api.booking;

import com.wardrobe.dao.booking.BookingDao;
import com.wardrobe.dao.goods.GoodsDao;
import com.wardrobe.dao.shop.ShopDao;
import com.wardrobe.dao.store.StoreDao;
import com.wardrobe.domain.booking.BookingShowBean;
import com.wardrobe.domain.goods.GoodsShowBean;
import com.wardrobe.domain.shop.ShopShowBean;
import com.wardrobe.domain.store.StoreShowBean;
import com.wardrobe.soa.utils.LocationUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Administrator on 2018/11/19.
 */
@Service
public class BookingService {
    @Autowired
    private BookingDao bookingDao;
    @Autowired
    private ShopDao shopDao;
    @Autowired
    private GoodsDao goodsDao;
    @Autowired
    private StoreDao storeDao;

    public List<BookingShowBean> getBookingList(){
        List<BookingShowBean> list = bookingDao.selectApiBookingList();
        list.stream().forEach(x->x.setShowPicture(x.getShowPicture().split(";")[0]));
        return list;
    }


    public BookingResponse bookingDetail(long id,double latitude,double longitude,long userId){
        BookingShowBean booking = bookingDao.selectBookingById(id);
        double distance = LocationUtils.getDistance(Double.parseDouble(booking.getLatitude()), Double.parseDouble(booking.getLongitude()), latitude, longitude);
        String disStr = LocationUtils.getDisStr("距离:", distance);
        ShopShowBean shop = shopDao.selectShopById(booking.getShopId());
        List<BookingShowBean> bookingShowBeans = bookingDao.selectBookingByShopId(booking.getShopId());
        StoreShowBean storeShowBean = storeDao.selectStoreBooking(userId, id);
        boolean flag = false;
        long storeId = 0;
        if (storeShowBean!=null){
                flag = true;
               storeId = storeShowBean.getId();
        }
        return BookingResponse.builder()
                .id(booking.getId())
                .showPicture(booking.getShowPicture().split(";"))
                .price(booking.getPrice())
                .name(booking.getName())
                .distance(disStr)
                .content(booking.getContent())
                .project(booking.getProject())
                .projectPrice(booking.getProjectPrice())
                .flag(flag)
                .storeId(storeId)
                .shopId(booking.getShopId())
                .shopLogo(shop.getLogo())
                .shopName(shop.getShopName())
                .telphone(shop.getTelphone())
                .address(shop.getAddress())
                .detailPicture(booking.getDetailPicture())
                .bookingShowBeanList(bookingShowBeans)
                .build();
    }
}
